<?php
/**
 * 公共 UploadImage Widget
 * User: Administrator
 * Date: 2016/5/31
 * Time: 23:17
 */

namespace common\widgets;


use yii\helpers\Html;

class UploadImage extends \yii\widgets\InputWidget
{
    /**
     * 传入的菜单数据，可以默认为空
     * @array
     */
    public $filedName = ""; //隐藏域名称
    public $type = "";
    public $imgWidth = 200; // 图片宽度
    public $imgHeight = 200; //图片高度
    public $value = '';

    public function init()
    {
        parent::init();

    }

    public function run()
    {
        parent::run();

        $id = empty($this->options['id']) ? mt_rand(1000, 9999) : $this->options['id'];

        if($this->hasModel()){
            $this->name = Html::getInputName($this->model, $this->attribute);
            $this->value = Html::getAttributeValue($this->model, $this->attribute);
        }

        if(empty($this->value)){
            $value = '';
            $src = '/assets/img/upload.png';
        }else{
            $value = $this->value;
            $src = $this->value;
        }

        echo '<div id="'.$id.'" class="upImageBox" style="width:'.$this->imgWidth.'px;overflow:hidden;">
                <div class="imgShowWrapper" data-tips="filename" >
                    <img class="imgShow" width="'.$this->imgWidth.'" height="'.$this->imgHeight.'" src="'.$src.'">
                </div>
                <input class="fileUploadInput hide" name="upImage" id="field-'.$id.'" type="file" multiple="multiple">
                <input type="hidden" class="inputField" name="'.$this->name.'" value="'.$value.'">
                <input type="button" class="btn btn-danger btn-sm btnCancel" value="取消缩略图"  style="width:'.$this->imgWidth.'px; margin-top: 4px">
            </div>';

        $js = '
            $("#'.$id.'").find(".btnCancel").on("click",function(){
                $(this).parent().find(".inputField").val("");
                $(this).parent().find(".imgShow").attr("src", "/assets/img/upload.png");
            });
        
            $("#'.$id.'").find(".imgShowWrapper").on("click",function(){
                $(this).parent().find(".fileUploadInput").trigger("click");
            });
        
             $("#'.$id.'").find(".fileUploadInput").change(function () {
                $.ajaxFileUpload({
                    type:"POST",
                    url:"/upload/qiniu.html",
                    secureuri:false,
                    fileElementId:"field-'.$id.'",
                    dataType: "json",
                    success: function (data){
                        if(parseInt(data.status) == 1){
                            $("#'.$id.'").parent().find(".imgShow").attr("src",data.data);
                            $("#'.$id.'").parent().find(".inputField").val(data.data);
                        }else{
                            App.alert({
                                type: "danger",
                                message: data.message,
                                icon: "fa fa-warning"
                            });
                        }},
                    error: function(){
                        App.alert({
                            type: "danger",
                            message: "网络超时",
                            icon: "fa fa-warning"
                        });
                    }
                });
            });
        ';
        $this->getView()->registerJs($js);
    }
}